package org.iabako.client.navigation;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import org.iabako.client.config.page.PageEnum;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.layout.widget.emailpreview.EmailPreview;
import org.iabako.client.ui.page.business.AddLink;
import org.iabako.client.ui.page.business.client.ClientCreate;
import org.iabako.client.ui.page.business.client.ClientModify;
import org.iabako.client.ui.page.business.client.ClientSearch;
import org.iabako.client.ui.page.business.communication.CommunicationCreate;
import org.iabako.client.ui.page.business.conversation.ConversationCreate;
import org.iabako.client.ui.page.business.conversation.ConversationModify;
import org.iabako.client.ui.page.business.expense.ExpenseMake;
import org.iabako.client.ui.page.business.expense.ExpenseModify;
import org.iabako.client.ui.page.business.expense.ExpenseSearch;
import org.iabako.client.ui.page.business.installment.InstallmentMake;
import org.iabako.client.ui.page.business.installment.InstallmentModify;
import org.iabako.client.ui.page.business.notification.NotificationConfig;
import org.iabako.client.ui.page.business.payment.PaymentMake;
import org.iabako.client.ui.page.business.payment.PaymentModify;
import org.iabako.client.ui.page.business.payment.PaymentTransfer;
import org.iabako.client.ui.page.business.paymentrequest.PaymentRequestConfig;
import org.iabako.client.ui.page.business.paymentrequest.PaymentRequestMailModify;
import org.iabako.client.ui.page.business.product.ProductCreate;
import org.iabako.client.ui.page.business.product.ProductModify;
import org.iabako.client.ui.page.business.product.ProductSearch;
import org.iabako.client.ui.page.business.product.StockManagement;
import org.iabako.client.ui.page.business.report.ReportCreate;
import org.iabako.client.ui.page.business.sale.*;
import org.iabako.client.ui.page.business.service.ServiceCreate;
import org.iabako.client.ui.page.business.service.ServiceModify;
import org.iabako.client.ui.page.business.service.ServiceSearch;
import org.iabako.client.ui.page.extranet.GoToDemo;
import org.iabako.client.ui.page.extranet.Login;
import org.iabako.client.ui.page.extranet.PasswordReminder;
import org.iabako.client.ui.page.extranet.SignUp;
import org.iabako.client.ui.page.home.HomePage;
import org.iabako.client.ui.page.support.AskQuestion;
import org.iabako.client.ui.page.support.Privacy;
import org.iabako.client.ui.page.support.ReportBug;
import org.iabako.client.ui.page.support.Terms;
import org.iabako.client.ui.page.user.*;
import org.iabako.client.ui.page.user.credit.BuyCredit;
import org.iabako.shared.exception.IabakoException;

import java.util.ArrayList;
import java.util.List;

/**
 * Class instance can not be created automatically because Class is not serializable
 * and Class.getInstance() method is not in JRE emulator library for GWT.
 * <p/>
 * The only way for instance Page Composite objects found is doing explicit Constructor calls,
 * which seems stupid (A better way should be found in the future).
 * <p/>
 * By now, we do it with this class.
 * <p/>
 * User: jose
 * Date: 24/11/13
 * Time: 18:42
 */
public class PageInstanceGetter {

    private static List<String> pageHistory = new ArrayList<String>();

    public static Composite getPageInstance(PageEnum pageEnum, AbstractForm parentForm) throws IabakoException {

        pageHistory.add(Window.Location.getHash());

        switch (pageEnum) {
            case IabakoHome:
                return new HomePage();
            case ClientCreate:
                return new ClientCreate(parentForm);
            case ClientModify:
                return new ClientModify(parentForm);
            case ClientSearch:
                return new ClientSearch();
            case ConversationCreate:
                return new ConversationCreate(parentForm);
            case ConversationModify:
                return new ConversationModify(parentForm);
            case ProductCreate:
                return new ProductCreate();
            case ProductModify:
                return new ProductModify();
            case ProductSearch:
                return new ProductSearch(true);
            case StockManagement:
                return new StockManagement();
            case ServiceCreate:
                return new ServiceCreate();
            case ServiceModify:
                return new ServiceModify();
            case ServicesSearch:
                return new ServiceSearch();
            case SaleMake:
                return new SaleMake(parentForm);
            case SaleGoTo:
                return new SaleGoTo();
            case SaleModify:
                return new SaleModify(parentForm);
            case SaleSearch:
                return new SaleSearch();
            case QuoteMake:
                return new QuoteMake();
            case QuoteModify:
                return new QuoteModify();
            case QuoteSearch:
                return new QuoteSearch();
            case PastSaleInstallmentSearch:
                return new PastInstallmentSearch();
            case PaymentModify:
                return new PaymentModify(parentForm);
            case PaymentMake:
                return new PaymentMake(parentForm);
            case PaymentTransfer:
                return new PaymentTransfer(parentForm);
            case InstallmentMake:
                return new InstallmentMake(parentForm);
            case InstallmentModify:
                return new InstallmentModify(parentForm);
            case ExpenseMake:
                return new ExpenseMake();
            case ExpenseModify:
                return new ExpenseModify();
            case ExpenseSearch:
                return new ExpenseSearch();
            case AddLinkToBusinessObject:
                return new AddLink(parentForm);
            case ReportCreate:
                return new ReportCreate();
            case CommunicationCreate:
                return new CommunicationCreate();
            case UserConfiguration:
                return new UserConfiguration();
            case UserCreate:
                return new UserCreate(parentForm);
            case UserModify:
                return new UserModify(parentForm);
            case ClientCustomFieldsConfiguration:
                return new ClientCustomFieldsConfiguration();
            case EnterpriseConfiguration:
                return new MyEnterprise();
            case NotificationConfiguration:
                return new NotificationConfig();
            case PaymentRequestConfiguration:
                return new PaymentRequestConfig();
            case PaymentRequestMail:
                return new PaymentRequestMailModify(parentForm);
            case EmailPreview:
                return new EmailPreview(parentForm);
            case Login:
                return new Login();
            case PasswordReminder:
                return new PasswordReminder();
            case GoToDemo:
                return new GoToDemo();
            case SignUp:
                return new SignUp();
            case BuyCredit:
                return new BuyCredit();
            case AskQuestion:
                return new AskQuestion();
            case ReportBug:
                return new ReportBug();
            case Terms:
                return new Terms(parentForm);
            case Privacy:
                return new Privacy(parentForm);
            default:
                throw new IllegalArgumentException();
        }
    }

    public static String getLastVisitedPages() {
        String result = "";
        for (int i = pageHistory.size(); i > 0 && i > pageHistory.size() - 10; i--) {
            result += pageHistory.get(pageHistory.size() - i) + "/";
        }
        return result;
    }
}
